# flask 数据库
# pip install flask-sqlalchemy




from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

class Config:
    '''配置参数'''
    SQLALCHEMY_DATABASE_URI = 'sqlite:////templates/db.sqlite3'
    SQLALCHEMY_TRACK_MODIFICATIONS = True

app.config.from_object(Config)

# 绑定到app
db = SQLAlchemy(app)

#创建数据模型类
class Role(db.Model):
    '''角色表'''
    __tablename__ = 'role'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(32),unique=True)

class User(db.Model):
    '''用户表'''
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128))
    # 表关系
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))

# @app.route('/index' )
# def index():
#     return render_template('index2.html' )


if __name__ == '__main__':
    '''清楚所有表'''
    db.drop_all()
    '''创建所有表'''
    db.create_all()
    '''创建对象 插入数据'''
    role1 = Role(name = 'admin')
    #session记录到对象任务
    db.session.add(role1)
    #提交任务
    db.session.commit()



    user1= User(name='zhang1',password='123',role_id=role1.id)
    user2= User(name='zhang2',password='123',role_id=role1.id)
    db.session.add_all([user1,user2])
    db.session.commit()

    # app.run()